﻿using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Cms.Web.UI;
using Telerik.Framework.Search;
using Telerik.Framework.Web;


namespace SelArom.Net.Sitefinity.IndexProviders.GenericContent
{
    public class ViewControl : CompositeControl, ISearchViewControl
    {
        #region Properties

        public string TemplatePath
        {
            get
            {
                object o = this.ViewState["TemplatePath"];
                //if (o == null)
                //    return "~/Sitefinity/Admin/ControlTemplates/CustomIndexViewControl.ascx";
                return (string)o;
            }
            set
            {
                this.ViewState["TemplatePath"] = value;
            }
        }

        public ITemplate ItemTemplate
        {
            get
            {
                if (this.itemTemplate == null)
                    this.itemTemplate = ControlUtils.GetTemplate<DefaultTemplate>(this.TemplatePath);
                return this.itemTemplate;
            }
        }

        #endregion

        #region Methods

        protected override void CreateChildControls()
        {
            this.container = new ControlContainer(this);
            this.ItemTemplate.InstantiateIn(this.container);
            this.Controls.Add(this.container);
        }

        #endregion

        #region Private Fields

        private ControlContainer container;
        private ITemplate itemTemplate;
        private IDictionary<string, string> settings;

        #endregion

        #region Default Template

        protected class DefaultTemplate : ITemplate
        {
            public void InstantiateIn(Control container)
            {
            }
        }

        #endregion

        #region Container

        protected class ControlContainer : GenericContainer<ViewControl>
        {
            public ControlContainer(ViewControl owner)
                : base(owner)
            {
            }
        }

        #endregion

        #region ISearchViewControl Members

        public void InitializeSettings(IDictionary<string, string> settings)
        {
            this.settings = settings;
        }

        #endregion
    }
}